package cn.jy.careuplusdoctor.base;

import cn.jy.careuplusdoctor.R;
import cn.jy.careuplusdoctor.injector.InjectorUitls;
import cn.jy.careuplusdoctor.main.LoginActivity;
import cn.jy.careuplusdoctor.main.MainActivity;
import cn.jy.careuplusdoctor.response.BaseResponse;
import cn.jy.careuplusdoctor.utils.AppUtils;
import cn.jy.careuplusdoctor.utils.LogUtils;
import cn.jy.careuplusdoctor.widgets.CareuTitleBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Message;
import android.text.TextUtils;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

public abstract class BaseActivity extends Activity implements Callback, Runnable {
	protected String TAG = this.getClass().getSimpleName();
	protected Handler mUiHandler;
	private Handler mNetworkHandler;
	private CareuTitleBar titleBar;
	private boolean networkAvailable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		AppUtils.getInstance().getAppManager().addActivity(this);
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}

	/*初始化控件*/
	protected void initCompenent(){};
	
	/**
	 * 设置标题栏，初始化标题栏成功后自动调用，子类不须手动调用
	 * @see method initTitleBar()
	 */
	protected void settingTitleBar(){};
	
	/**
	 * UI注射成功后自动调用，子类不须手动调用
	 * @see method onInJectView()
	 */
	protected void onAfterInJector(){};
	
	protected void initTitleBar() {
		if (titleBar == null) {
			titleBar = new CareuTitleBar(this);
		}
		settingTitleBar();
	}
	/**
	 * 注射Activity View
	 */
	protected void onInJectView() {
		InjectorUitls.getInstance().injectAllView(this);
		onAfterInJector();
	}

	protected void initUtils() {}

	public CareuTitleBar getTitleBar() {
		return titleBar;
	}

	public void setTitleBar(CareuTitleBar titleBar) {
		this.titleBar = titleBar;
	}

	protected void initHandler() {
		initUiHandler();
		initHandlerThread();
	}

	protected void initUiHandler() {
		mUiHandler = new Handler(this);
	}

	protected void initHandlerThread() {
		HandlerThread ht = new HandlerThread("ht");
		ht.start();
		mNetworkHandler = new Handler(ht.getLooper());
	}

	protected void postRunnable() {
		postRunnableDelay(0);
	}

	protected void postRunnableDelay(int delayMillis) {
		postRunnableDelay(this, delayMillis);
	}

	protected void postRunnableDelay(Runnable r, int delayMillis) {
		mNetworkHandler.postDelayed(r, delayMillis);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		AppUtils.getInstance().getAppManager().finishActivity(this);
		super.onDestroy();
	}

	protected void showInfoLog(String msg)
	{
		showInfoLog(TAG, msg);
	}
	protected void showInfoLog(String tag,String msg)
	{
		LogUtils.i(tag, msg);
	}
	
	protected void showDebugLog(String msg)
	{
		showDebugLog(TAG, msg);
	}
	protected void showDebugLog(String tag,String msg)
	{
		LogUtils.d(tag, msg);
	}
	
	protected void longToast(int resid) {
		Toast.makeText(this, resid, Toast.LENGTH_LONG).show();
	}

	protected void longToast(String mToast) {
		Toast.makeText(this, mToast, Toast.LENGTH_LONG).show();
	}

	protected void shortToast(String mToast) {
		Toast.makeText(this, mToast, Toast.LENGTH_SHORT).show();
	}

	protected void shortToast(int resid) {
		Toast.makeText(this, resid, Toast.LENGTH_SHORT).show();
	}

	protected void openActivity(Class<?> mClass) {
		openActivity(mClass, null);
	}
	
	private ProgressDialog mDialog = null;
	protected void showLoading(String msg)
	{
		showLoading("",msg);
	}
	
	protected void showLoading(int msgResId)
	{
		String msg = getString(msgResId);
		showLoading("",msg);
	}
	
	protected void showLoading(int titleId,String msg)
	{
		String title = getString(titleId);
		showLoading(title,msg);
	}
	
	protected void showLoading(String title,String msg)
	{
		if(mDialog == null)
		{
			mDialog = new ProgressDialog(this);
		}
		mDialog.setTitle(title);
		mDialog.setMessage(msg);
		mDialog.show();
	}
	
	protected void hidenLoading()
	{
		if (mDialog !=null) {
			mDialog.dismiss();
		}
	}

	protected void openActivity(Class<?> mClass, Bundle mData) {
		Intent mIntent = new Intent(this, mClass);
		if (mData != null) {
			mIntent.putExtras(mData);
		}
		startActivity(mIntent);
	}

	protected void openActivity(String mAction) {
		openActivity(mAction, null);
	}
	
	protected void openActivity(Intent intent) {
		if (intent == null) {
			return;
		}
		startActivity(intent);
	}

	protected void openActivity(String mAction, Bundle mData) {
		Intent mIntent = new Intent(mAction);
		if (mData != null) {
			mIntent.putExtras(mData);
		}
		startActivity(mIntent);
	}

	protected void defaultFinish() {
		super.finish();
		overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	}

	protected void appExit() {
		defaultFinish();
		/* release RAM */
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);

		/* kill Process */
		System.exit(0);
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	protected void hidenKeybord() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(this.getWindow().getDecorView()
				.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}

	@Override
	public void onBackPressed() {
		if (this instanceof LoginActivity || this instanceof MainActivity) {
			onDoublePressExitApp();
		}
		else {
			defaultFinish();

		}
	
	}

	long mExitTime = System.currentTimeMillis();;
	private void onDoublePressExitApp() {
		if ((System.currentTimeMillis() - mExitTime) > 2000) {
			shortToast("再按一次返回退出程序");
			mExitTime = System.currentTimeMillis();
		} else {
			appExit();
		}
	}
	
	public boolean isNetworkAvailable() {
		return networkAvailable;
	}
	
	public void onConnection()
	{
		networkAvailable = true;
	}
	
	public void onDisconnection()
	{
		networkAvailable = false;
	}
	
	/**
	 * http 响应是否成功
	 * @param resType
	 * @return
	 */
	protected boolean isHttpSuccess(String resType)
	{
		if (TextUtils.isEmpty(resType)) {
			return false;
		}
		return TextUtils.equals("1", resType.trim());
	}
	
	/**
	 * http 响应是否成功
	 * @param resType
	 * @return
	 */
	protected boolean isHttpSuccess(BaseResponse data)
	{
		if (data == null) {
			shortToast(R.string.http_error);
			return false;
		}
		else if (isHttpSuccess(data.getResType())) {
			return true;
		}
		else {
			shortToast(data.getMsg());
			return false;
		}
	}
	
	protected void badHttpRequest(BaseResponse data)
	{
		if (data == null) {
			shortToast(R.string.http_error);
		}
		else {
			shortToast(data.getMsg());
		}
	}
	
	// 处理UI刷新
	@Override
	public boolean handleMessage(Message msg) {
		// TODO Auto-generated method stub
		return false;
	}

	// 处理网络请求
	@Override
	public void run() {
		// TODO Auto-generated method stub

	}

}
